package com.weetop.cfw.mine.activity

import android.os.Bundle
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.event.RequestLeaseSaleEvent
import com.weetop.cfw.mine.fragment.AllLeaseSaleInformationFragment
import com.weetop.cfw.mine.fragment.PublishedSaleInformationFragment
import com.weetop.cfw.mine.fragment.ToBeAuditedLeaseSaleInformationFragment
import com.weetop.cfw.utils.EventBusUtils
import kotlinx.android.synthetic.main.activity_lease_sale_information.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 出租出售信息界面
 */
class LeaseSaleInformationActivity : CommonBaseActivity(),View.OnClickListener {
    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageLeaseSaleInformationBack -> ActivityUtils.finishActivity(this)
        }
    }

    companion object{
        const val KEY = "LeaseSaleInformationKind"
    }

    private val leaseSaleInformationTitleList = listOf(
        "全部","待审核的","已发布的"
    )

    private lateinit var allLeaseSaleInformationFragment: AllLeaseSaleInformationFragment//全部交易信息Fragment
    private lateinit var publishLeaseSaleInformationFragment: PublishedSaleInformationFragment//已发布的交易信息Fragment
    private lateinit var toBeAuditedLeaseSaleInformationFragment: ToBeAuditedLeaseSaleInformationFragment//待审核的交易信息Fragment

    private lateinit var leaseSaleInformationFragmentList:List<CommonBaseFragment>

    private lateinit var leaseSaleInformationPagerAdapter: LeaseSaleInformationPagerAdapter

    override fun getLayoutId() = R.layout.activity_lease_sale_information

    override fun initView(savedInstanceState: Bundle?) {
        setViewsOnClickListener(this,imageLeaseSaleInformationBack)

       allLeaseSaleInformationFragment = AllLeaseSaleInformationFragment()
        publishLeaseSaleInformationFragment = PublishedSaleInformationFragment()
        toBeAuditedLeaseSaleInformationFragment = ToBeAuditedLeaseSaleInformationFragment()

    }

    override fun initData(savedInstanceState: Bundle?) {
      leaseSaleInformationFragmentList = listOf(allLeaseSaleInformationFragment,toBeAuditedLeaseSaleInformationFragment,publishLeaseSaleInformationFragment)

      leaseSaleInformationPagerAdapter = LeaseSaleInformationPagerAdapter(supportFragmentManager,leaseSaleInformationFragmentList,leaseSaleInformationTitleList)
      leaseSaleInformationViewPager.offscreenPageLimit = 3
      leaseSaleInformationViewPager.adapter = leaseSaleInformationPagerAdapter

      initTabLayout()

      val currentItem = intent.getIntExtra(KEY,0)
      LogUtils.d("currentItem",currentItem)
      leaseSaleInformationViewPager.currentItem = currentItem
    }

    private fun initTabLayout(){
        leaseSaleInformationIndicator.addTab(leaseSaleInformationIndicator.newTab().setText(leaseSaleInformationTitleList[0]))
        leaseSaleInformationIndicator.addTab(leaseSaleInformationIndicator.newTab().setText(leaseSaleInformationTitleList[1]))
        leaseSaleInformationIndicator.addTab(leaseSaleInformationIndicator.newTab().setText(leaseSaleInformationTitleList[2]))

        leaseSaleInformationIndicator.setupWithViewPager(leaseSaleInformationViewPager)
    }


    private class LeaseSaleInformationPagerAdapter(fragmentManager: FragmentManager,var leaseSaleInformationFragmentList:List<CommonBaseFragment>,var titleList:List<String>):FragmentStatePagerAdapter(fragmentManager){

        override fun getItem(position: Int) = leaseSaleInformationFragmentList[position]

        override fun getCount() = leaseSaleInformationFragmentList.size

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titleList[position]
        }

    }

}
